home *** CD-ROM | disk | FTP | other *** search
/ The World of Computer Software / The World of Computer Software.iso / snpd1292.zip / CENTER.C < prev    next >
C/C++ Source or Header  |  1992-12-26  |  1KB  |  50 lines

  1. /* public domain by Jerry Coffin.  Tested with MSC 7.0  */
  2. /* written primarily for clarity, not speed...          */
  3. /* requires w_wrap.c and w_wrap.h from snippets.        */
  4.  
  5.  
  6. #include <stdio.h>
  7. #include <conio.h>
  8. #include <string.h>
  9. #include "w_wrap.h"
  10.  
  11. void center(FILE *file, char *string, size_t width)
  12. {
  13.       char *line,*end_line;
  14.       size_t spaces;
  15.       int last = 0;
  16.       size_t len;
  17.  
  18.       word_wrap(string,width);
  19.       line = string;
  20.       while (!last)
  21.       {
  22.             end_line = strchr(line,'\n');
  23.             if (NULL==end_line)
  24.             {
  25.                   last = 1;
  26.                   end_line = strchr(line,'\0');
  27.             }
  28.             len = end_line - line;
  29.             spaces = (width - len) / 2 ;
  30.             fprintf(file,"\n%*c%*.*s",spaces,' ',len,len,line);
  31.             line = end_line + 1;
  32.       }
  33. }
  34.  
  35. #ifdef TEST
  36.  
  37. int main(void)
  38. {
  39.       char *string = "This is a long string to see if it will be"
  40.             " printed out correctly but I'm not sure whether it will work"
  41.             " correctly or not.  I guess we'll see when we try it out.";
  42.  
  43.       printf("\nHere's the string centered in 50 columns.\n");
  44.       center(stdout,string,50);
  45.       printf("\n\nAnd here it's centered in 72 columns.\n");
  46.       center(stdout,string,72);
  47. }
  48.  
  49. #endif
  50.